﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using ZaidimasSandelys.Models.DataStructures;

namespace ZaidimasSandelys.CustomDrawTools
{
    public class ContainerVisual : AllocatedVisual
    {
        /// <summary>
        /// Klasė pakuria šitą įvykį užkrovus visus žaidimo duomenis.
        /// Papildomų duomenų nereikia: parametre <code>sender</code> yra visa reikalinga informacija
        /// </summary>
        public event EventHandler<EventArgs> MovedOnTarget;

        /// <summary>
        /// Elementų sąrašas reikalingas žaidimo pabaigos nustatymui
        /// </summary>
        public IEnumerable<AllocatedVisual> AllGameElements { get; set; }
        
        private bool _IsContainerOnTarget;
        public bool IsContainerOnTarget 
        {
            get { return _IsContainerOnTarget; }
            protected set { _IsContainerOnTarget = value; }
        }

        public ContainerVisual(short x, short y) : base(x, y, VisualType.Container)
        {
            this.CanMove = true;
            _IsContainerOnTarget = false;
        }

        public override void DrawItself(Size multiplicator)
        {
#if DEBUG
            this.Dispatcher.VerifyAccess();
#endif
            DrawItself(multiplicator, GetBitmapBrush("deze-medine.png"), new Pen(Brushes.Gray, 1));
        }

        public virtual void Move(Movement delta, Size multiplicator)
        {
            this.Position = this.Position + delta;
            if (AllGameElements.Any(x => x.ImgType == VisualType.Target && x.Position == this.Position))
                IsContainerOnTarget = true;
            
            string imgId = IsContainerOnTarget ? "deze-medine.png" : "deze-medine.png";
            DrawItself(multiplicator, GetBitmapBrush(imgId), new Pen(Brushes.Gray, 1));
            
            if (IsContainerOnTarget && MovedOnTarget != null)
                MovedOnTarget(this, new EventArgs());
        }
    }

    public class HeavyContainerVisual : ContainerVisual
    {
        public new event EventHandler<EventArgs> MovedOnTarget;

        public HeavyContainerVisual(short x, short y) : base(x, y)
        {
            this.ImgType = VisualType.HeavyContainer;
        }

        public override void DrawItself(Size multiplicator)
        {
#if DEBUG
            this.Dispatcher.VerifyAccess();
#endif
            DrawItself(multiplicator, GetBitmapBrush("deze-metaline.png"), new Pen(Brushes.Gray, 1));
        }

        public override void Move(Movement delta, Size multiplicator)
        {
            this.Position = this.Position + delta;
            if (AllGameElements.Any(x => x.ImgType == VisualType.TargetHC && x.Position == this.Position))
                IsContainerOnTarget = true;
            
            string imgId = "deze-metaline.png";
            DrawItself(multiplicator, GetBitmapBrush(imgId), new Pen(Brushes.Gray, 1));

            if (IsContainerOnTarget && MovedOnTarget != null)
                MovedOnTarget(this, new EventArgs());
        }
    }
}
